﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace Signage01.dao
{
    class ScreenTemplateDAO
    {
        string serializationFile = "resources/screen/screentemplate.bin";
        //private List<Timeline> listTimeline;
        static ScreenTemplateDAO screenTemplateDAO = null;

        private ScreenTemplateDAO()
        {

        }

        public static ScreenTemplateDAO getSingleTon()
        {
            if (screenTemplateDAO == null)
            {
                screenTemplateDAO = new ScreenTemplateDAO();
            }
            return screenTemplateDAO;
        }


        public List<ScreenTemplate> getAllByScreen(Screen screenTemp)
        {
            // retrieving the list from the file
            try
            {
                using (Stream stream = File.Open(serializationFile, FileMode.OpenOrCreate))
                {
                    var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                    // if this line throws exception, the file is empty, so we have to create a new list in catch block
                    List<ScreenTemplate> listTemp = (List<ScreenTemplate>)bformatter.Deserialize(stream);
                    List<ScreenTemplate> listScreenTemplateFiltered = new List<ScreenTemplate>();

                    foreach (ScreenTemplate st in listTemp)
                    {
                        if (st.ScreenObject.ScreenName == screenTemp.ScreenName)
                        {
                            listScreenTemplateFiltered.Add(st);
                        }
                    }

                    return listScreenTemplateFiltered;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("The file is empty");
                List<ScreenTemplate> listTemp = new List<ScreenTemplate>();
                return listTemp;
            }
        }


        public  List<ScreenTemplate>  getAll()
        {
            // retrieving the list from the file
            try
            {
                using (Stream stream = File.Open(serializationFile, FileMode.OpenOrCreate))
                {
                    var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

                    // if this line throws exception, the file is empty, so we have to create a new list in catch block
                    List<ScreenTemplate> listTemp = (List<ScreenTemplate>)bformatter.Deserialize(stream);
                    return listTemp;
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("The file is empty");
                List<ScreenTemplate> listTemp = new List<ScreenTemplate>();
                return listTemp;
            }
        }

        private List<String> getScreenTemplateNames()
        {
            List<String> listScreenTemplateName = new List<String>();

            try
            {
                List<ScreenTemplate> listScreenTemplate = getAll();

                foreach (ScreenTemplate tempScreenTemplate in listScreenTemplate)
                {
                    listScreenTemplateName.Add(tempScreenTemplate.Name);
                }
                return listScreenTemplateName;

            }
            catch (Exception ex)
            {
                List<String> tempTimelineListName = new List<String>();
                return tempTimelineListName;

            }
        }

        public bool screenTemplateNameExists(String name)
        {

            List<String> listScreenTemplateName = getScreenTemplateNames();

            foreach (String tempName in listScreenTemplateName)
            {
                if (name == tempName)
                {
                    return true;
                }
            }
            return false;
        }


        public int saveScreenTempldateObject(ScreenTemplate newScreenTemplateObj)
        {
            List<ScreenTemplate> listScreenTemplate = getAll();
            listScreenTemplate.Add(newScreenTemplateObj);

            return saveListToFile(listScreenTemplate);
        }

        public int deleteTimelineObject(Timeline timeline)
        {
            List<ScreenTemplate> listScreenTemplate = getAll();

            int index = 0;
            int deleteIndex = -1;

            foreach (ScreenTemplate tempScreenTemplate in listScreenTemplate)
            {
                if (timeline.Name == tempScreenTemplate.Name)
                {
                    deleteIndex = index;
                    break;
                }
                index++;
            }

            listScreenTemplate.RemoveAt(deleteIndex);

            return saveListToFile(listScreenTemplate);
        }

        public int saveListToFile(List<ScreenTemplate> listScreenTemplate)
        {
            try
            {
                // saving the list to the file
                using (Stream stream = File.Open(serializationFile, FileMode.OpenOrCreate))
                {
                    var bformatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
                    bformatter.Serialize(stream, listScreenTemplate);
                    stream.Close();
                }
                return 1;
            }
            catch (Exception ex)
            {
                Console.WriteLine("save excepiton is:" + ex.ToString());
                return 0;
            }
        }


    }
}
